package ruk.applications.copyhelper.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.*;

public class CopyHelper implements EntryPoint {
    private DataListBox dataListBox;
    private ClipboardTimer timer;
    private TextArea selectedDataTextArea;

    public void onModuleLoad() {
        RootPanel rootPanel = RootPanel.get();

        try {
            JavaScriptUtils.getClipboardData();
            showCopyHelper(rootPanel);
        }
        catch (Exception e) {
            showClipboardAccessNotSupportedMessage(rootPanel);
        }
    }

    private void showClipboardAccessNotSupportedMessage(RootPanel rootPanel) {
        Label label = new Label("Clipboard Access is not supported in your browser !. Please use Internet Explorer for now.");
        setId(label, "clipboardNotAccessible");
        rootPanel.add(label);
    }

    private void showCopyHelper(RootPanel rootPanel) {
        HorizontalPanel mainPanel = new HorizontalPanel();
        mainPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
        mainPanel.setSpacing(30);
        rootPanel.add(mainPanel);

        dataListBox = new DataListBox();
        setId(dataListBox, "dataListBox");
        dataListBox.setVisibleItemCount(10);
        dataListBox.setWidth("300px");
        dataListBox.setHeight("400px");

        mainPanel.add(dataListBox);

        VerticalPanel rightPanel = new VerticalPanel();

        selectedDataTextArea = new TextArea();
        setId(selectedDataTextArea, "selectedDataTextArea");
        selectedDataTextArea.setWidth("300px");
        selectedDataTextArea.setHeight("350px");

        rightPanel.add(selectedDataTextArea);

        Button copyButton = new Button();
        setId(copyButton, "copyButton");
        copyButton.setText("Copy");

        rightPanel.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);
        rightPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
        rightPanel.add(copyButton);
        rightPanel.setCellHeight(copyButton, "40px");

        mainPanel.add(rightPanel);
        mainPanel.setStyleName("backgroundColor");

        initializeTimer();

        dataListBox.addChangeListener(new ChangeListener() {
            public void onChange(Widget sender) {
                int selectedIndex = dataListBox.getSelectedIndex();
                if (selectedIndex != -1)
                    selectedDataTextArea.setText(dataListBox.getValue(selectedIndex));
            }
        });

        copyButton.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                String selectedData = selectedDataTextArea.getText();
                if (!"".equals(selectedData))
                    JavaScriptUtils.setClipboardData(selectedData);
            }
        });
    }

    private void setId(final UIObject dataListBox, final String id) {
        DOM.setElementAttribute(dataListBox.getElement(), "id", id);
    }

    private void initializeTimer() {
        timer = new ClipboardTimer(dataListBox, 2000);
        timer.start();
    }
}
